package cn.minimelon.cushaw.impl;

import cn.minimelon.bittermelon.bean.LoginUser;
import cn.minimelon.bittermelon.utils.Melon;
import cn.minimelon.cushaw.dao.SysDatadefDao;
import cn.minimelon.cushaw.entity.SysDatadef;
import cn.minimelon.cushaw.entity.SysFields;
import cn.minimelon.cushaw.model.DatadefReq;
import cn.minimelon.cushaw.service.SysDatadefService;
import cn.minimelon.cushaw.service.SysFieldsService;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SqlId;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.ext.solon.Db;
import org.noear.solon.annotation.Inject;
import org.noear.solon.extend.aspect.annotation.Service;

import java.util.List;

@Service
public class SysDatadefServiceImpl implements SysDatadefService {
    @Db
    SysDatadefDao datadefDao;

    @Inject
    SysFieldsService fieldsService;

    @Inject
    SQLManager sqlManager;

    @Override
    public SysDatadef selectByCode(String code) {
        LambdaQuery<SysDatadef> query = sqlManager.lambdaQuery(SysDatadef.class);
        SysDatadef datadef = query.andEq(SysDatadef::getCode, code).single();
        List<SysFields> fieldList = fieldsService.selectByDefCode(code);
        datadef.getFieldList().addAll(fieldList);
        return datadef;
    }

    @Override
    public SysDatadef selectById(String uid) {
        return datadefDao.single(uid);
    }

    @Override
    public List<SysDatadef> selectByArea(String area) {
        return datadefDao.selectByArea(area);
    }

    @Override
    public PageResult<SysDatadef> selectPage(DatadefReq req) {
        SqlId selectById = SqlId.of("SysDatadefMapper","selectByArea");
        return sqlManager.pageQuery(selectById, SysDatadef.class, req, Melon.PR(req));
    }

    @Override
    public void insert(SysDatadef dataDef, LoginUser user) {
        Melon.setInsertInfo(dataDef, user);
        datadefDao.insert(dataDef);
    }

    @Override
    public int update(SysDatadef datadef, LoginUser user) {
        Melon.setUpdateInfo(datadef, user);
        return datadefDao.updateById(datadef);
    }
}
